package com.juphoon.mockdata.util;


import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.net.InetAddress;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author juphoon
 * @date 2022/4/2
 */
public class Utils {

    public final static String RECORD_PATTERN = "yyyyMMddHHmmss";
    public final static String EVENT_PATTERN = "yyyyMMdd HH:mm:ss.SSSZ";
    public final static String FILE_DIR_PATTERN = "yyyy-MM-dd";
    private static String HOME = "";
    private static Snowflake snowflake = IdUtil.getSnowflake(1, 1);

    public static String getTime(String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.getDefault());
        return formatter.format(new Date());
    }

    public static String getTime(String pattern, long timems) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.getDefault());
        return formatter.format(new Date(timems));
    }

    public static String getFileContent(String path) {
        String content = "";
        try {
            FileReader fileReader = new FileReader(path);
            if (fileReader.getFile().exists()) {
                content = fileReader.readString();
            }
        } catch (Exception e) {

        }
        return content;
    }

    public static String getRunDir() {
        return System.getProperty("user.dir");
    }

    public static String genUniqueNumber() {
        return snowflake.nextIdStr();
    }

    public static String ifNullReturnBlank(String content) {
        return content == null ? "" : content;
    }

    public static String ifNullReturnBlank(Timestamp timestamp) {
        return timestamp == null ? "" : timestamp.toString();
    }

    public static int ifNullReturnInt(Integer value, int ret) {
        return value == null ? ret : value;
    }

    public static String ifBlankReturnListStr(String content) {
        return StrUtil.isBlank(content) ? "[]" : content;
    }

    public static String ifBlankReturnObjStr(String content) {
        return StrUtil.isBlank(content) ? "{}" : content;
    }

    /**
     * 目前插入数据库都用'，因此要处理字符串里的'，还要处理 \
     *
     * @return
     */
    public static String dealSqlStr(String sqlStr) {
        return sqlStr.replace("'", "''").replace("\\", "\\\\");
    }

    public static String getIpAddr(HttpServletRequest request) {
        try {
            String ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (StrUtil.equals(ipAddress, "127.0.0.1") || StrUtil.equals(ipAddress, "0:0:0:0:0:0:0:1")) {
                    //根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
            return ifNullReturnBlank(ipAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将字符串切割成多段字符
     *
     * @param src
     * @param length
     * @return
     */
    public static String[] stringToStringArray(String src, int length) {
        // 检查参数是否合法
        if (null == src || src.equals("")) {
            return null;
        }
        if (length <= 0) {
            return null;
        }
        // 获取整个字符串可以被切割成字符子串的个数
        int n = (src.length() + length - 1) / length;
        String[] split = new String[n];
        for (int i = 0; i < n; i++) {
            if (i < (n - 1)) {
                split[i] = src.substring(i * length, (i + 1) * length);
            } else {
                split[i] = src.substring(i * length);
            }
        }
        return split;
    }

}
